package org.ysu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.ysu.entity.Poor;
import org.ysu.mapper.PoorMapper;
import org.ysu.service.PoorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.ysu.vo.ResultVO;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 贫人员表 服务实现类
 * </p>
 *
 * @author LiuJunDa
 * @since 2024-08-16
 */
@Service
public class PoorServiceImpl extends ServiceImpl<PoorMapper, Poor> implements PoorService {

    private PoorMapper poorMapper;
    @Override
    public Boolean add(String poorPeople) {
        List<Poor> poors = JSONObject.parseArray(poorPeople, Poor.class);
        return this.saveBatch(poors);
    }

    @Override
    public boolean check(String value, Long id, String field) {
        QueryWrapper<Poor> wrapper = new QueryWrapper<>();
        wrapper.eq(field,value);
        if(id != null){
            wrapper.ne("id",id);
        }
        return this.count(wrapper) == 0;
    }

    @Override
    public List<Poor> list(Long fId) {
        QueryWrapper<Poor> wrapper = new QueryWrapper<>();
        wrapper.eq("f_id",fId);
        return this.list(wrapper);
    }

    @Override
    public Boolean addOne(String idNumber, String name, String nation, Integer sex, Boolean isHead, String relation, String work, Long fId, String health, Boolean insurance, Boolean basic) {
        return this.save(new Poor(
                idNumber,
                name,
                nation,
                sex,
                isHead,
                relation,
                work,
                fId,
                health,
                insurance,
                basic
        ));
    }

    @Override
    public Boolean update(Long id, String idNumber, String name, String nation, Integer sex, Boolean isHead, String relation, String work, Long fId, String health, Boolean insurance, Boolean basic) {
        return this.updateById(new Poor(
                id,
                idNumber,
                name,
                nation,
                sex,
                isHead,
                relation,
                work,
                health,
                fId,
                insurance,
                basic
        ));
    }

    @Override
    @Transactional
    public Boolean setHead(Long id, Long fId) {
        QueryWrapper<Poor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_id",fId);
        queryWrapper.eq("is_head",1);
        Poor oldHead = new Poor();
        oldHead.setIsHead(false);
        oldHead.setRelation("");
        this.update(oldHead,queryWrapper);

        Poor newHead = new Poor();
        newHead.setIsHead(true);
        newHead.setRelation("户主本人");
        QueryWrapper<Poor> queryWrapperNew = new QueryWrapper<>();
        queryWrapperNew.eq("id",id);
        return  this.update(newHead,queryWrapperNew);
    }
}
